﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ComputerStoreManager;
using Modules.Core;
using Modules.Library.Entities;

namespace ComputerStoreManager.ViewModel {
    public class AddEmployeeControlViewModel : BasePersonControlViewModel {

        #region Variables
        private RadioButton m_rdoMale;
        private RadioButton m_rdoFemale;
        private Image imageAnhDaiDien;
        private Button btnAddPicture;
        private Button btnAddEmp;
        private Button btnClear;
        private ComboBox cbbTypeOfEmp;

        private bool m_isVisibleAddEmpPanel = false;

        private List<ComboBoxItem> m_listTypeOfEmpSource = new List<ComboBoxItem>();
        #endregion

        #region Properties
        public bool IsVisibleAddEmpPanel {
            get { return m_isVisibleAddEmpPanel; }
            set { m_isVisibleAddEmpPanel = value; OnPropertyChanged("IsVisibleAddEmpPanel"); }
        }

        public List<ComboBoxItem> TypeOfEmpSource {
            get { return m_listTypeOfEmpSource; }
            set { m_listTypeOfEmpSource = value; OnPropertyChanged("TypeOfEmpSource"); }
        }
        #endregion

        #region ICommand
        public ICommand AddPictureCommand {
            get;
            private set;
        }
        public ICommand AddEmpCommand {
            get;
            private set;
        }
        public ICommand ClearCommand {
            get;
            private set;
        }

        #endregion

        #region Init

        public override void InitControlInfo() {
            if (TypeOfEmpSource.Count > 0) {
                TypeOfEmpSource.Clear();
            }
            DataTable data = Model.GetInitInfo(KindOfObject.BY_EMPLOYEE);

            foreach (DataRow row in data.Rows) {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = (string)row["TenLoaiNV"];
                item.Tag = (string)row["MaLoaiNV"];
                TypeOfEmpSource.Add(item);
            }

            base.TxtPersonId.Text = Converter.ConvertToId(Model.GetThongSoPhu(KindOfObject.BY_EMPLOYEE));
        }

        private void InitCmd() {
            AddPictureCommand = new ExecutableCommand(DoAddPicture);
            AddEmpCommand = new ExecutableCommand(DoAddNewPerson);
            ClearCommand = new ExecutableCommand(DoClearFields);
        }

        #endregion

        #region Methods
        public override bool CheckIsNotNull() {
            return base.CheckIsNotNull();
        }

        public override void DoAddNewPerson()
        {
            try
            {

                if (CheckIsNotNull())
                {
                    Employee emp = new Employee();
                    emp.MaNhanVien = base.TxtPersonId.Text;
                    emp.SoCMND = base.TxtIdentifier.Text;
                    emp.HoVaTen = base.TxtPersonFullName.Text;
                    emp.DiaChi = base.TxtAddress.Text;
                    emp.SoDienThoai = base.TxtPhone.Text;
                    emp.Email = base.TxtEmail.Text;
                    emp.UserName = base.TxtUserName.Text;
                    emp.PassWord = base.TxtPassword.Password;
                    emp.MaLoaiNhanVien = ((ComboBoxItem)cbbTypeOfEmp.SelectedItem).Tag.ToString();
                    emp.NgaySinh = DateTime.Parse(Converter.ConvertToDateTime(base.TxtDateOfBirth.Text, Converter.MMDDYYYY));
                    emp.GioiTinh = m_rdoMale.IsChecked == true ? "Nam" : "Nu";
                    emp.AnhDaiDien = "Chưa cập nhật";

                    if (!Model.InsertNewData(emp, KindOfObject.BY_EMPLOYEE))
                    {
                        MessageBox.Show(Model.Error);
                    }
                    else
                    {
                        DoClearFields();
                        MessageBox.Show(Constants.SUCCESS);
                    }
                }
                else
                {
                    MessageBox.Show("Bạn không được bỏ trống bất kỳ trường nào.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoAddPicture() {
            MessageBox.Show("Đang xây dựng");
        }

        public override void DoClearFields() {
            try
            {
                base.DoClearFields();
                this.m_rdoMale.IsChecked = true;
                this.m_rdoFemale.IsChecked = false;
                this.imageAnhDaiDien.Source = null;
                InitControlInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        #endregion

        #region Constructor

        public AddEmployeeControlViewModel(TextBox txtEmpId, TextBox txtFullName, TextBox txtDateOfBirth, TextBox txtIdentifier,
            RadioButton m_rdoMale, RadioButton m_rdoFemale, TextBox txtAddress, TextBox txtPhone, TextBox txtEmail,
            TextBox txtUserName, PasswordBox txtPassword, Image imageAnhDaiDien, Button btnAddPicture, Button btnAddEmp, Button btnClear, ComboBox cbbTypeOfEmp)
            : base(txtFullName, txtEmpId, txtDateOfBirth, txtIdentifier, txtPhone, txtAddress, txtEmail, txtUserName, txtPassword) {

            // TODO: Complete member initialization
            this.m_rdoMale = m_rdoMale;
            this.m_rdoFemale = m_rdoFemale;
            this.imageAnhDaiDien = imageAnhDaiDien;
            this.btnAddPicture = btnAddPicture;
            this.btnAddEmp = btnAddEmp;
            this.btnClear = btnClear;
            this.cbbTypeOfEmp = cbbTypeOfEmp;

            InitCmd();
            InitControlInfo();
        }

        #endregion
    }
}
